﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using DTcms.Common;

namespace DTcms.BLL
{
    /// <summary>
    ///会员主表
    /// </summary>
    public partial class users
    {
        //private readonly Model.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly DAL.users dal;

        public users()
        {
            dal = new DAL.users("dt_");
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.users model)
        {
            return dal.Add(model);
        }

        public int Add(string user_name, string password, string mobile, string avatar, int province_id, int city_id, int district_id, int company_id)
        {
            Model.users model = new Model.users();

            model.group_id = 1;
            model.user_name = user_name;
            model.salt = Utils.GetCheckCode(6);
            model.password = DESEncrypt.Encrypt(password, model.salt).ToString();
            model.avatar = avatar;
            model.mobile = mobile;
            model.province_id = province_id;
            model.city_id = city_id;
            model.district_id = district_id;
            model.amount = 0M;
            model.point = 0;
            model.status = 0;
            model.reg_time = DateTime.Now;
            model.company_id = company_id;

            int result = dal.Add(model);
            return result;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.users model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.users GetModel(int id)
        {
            return dal.GetModel(id);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetListForExport(int Top, string strWhere, string filedOrder)
        {
            return dal.GetListForExport(Top, strWhere, filedOrder);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            return dal.GetList(pageSize, pageIndex, strWhere, filedOrder, out recordCount);
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 返回信息标题
        /// </summary>
        public string GetUserName(int id)
        {
            return dal.GetUserName(id);
        }
        /// <summary>
        /// 根据公司ID获取用户名
        /// </summary>
        public string GetCompanyUserName(int company_id)
        {
            return dal.GetCompanyUserName(company_id);
        }
        public int GetUserID(int company_id)
        {
            return dal.GetUserID(company_id);
        }

        public int GetUserID(string mobile)
        {
            return dal.GetUserID(mobile);
        }
        /// <summary>
        /// 返回手机号
        /// </summary>
        public string GetMobile(int id)
        {
            return dal.GetMobile(id);
        }
        /// <summary>
        /// 返回头像
        /// </summary>
        public string GetAvatar(int id)
        {
            return dal.GetAvatar(id);
        }
        /// <summary>
        /// 根据会员ID返回GROUPID
        /// </summary>
        public int GetGroupID(int id)
        {
            return dal.GetGroupID(id);
        }
        /// <summary>
        /// 检查用户名是否存在
        /// </summary>
        public bool Exists(string user_name)
        {
            return dal.Exists(user_name);
        }

        /// <summary>
        /// 检查同一IP注册间隔(小时)内是否存在
        /// </summary>
        public bool Exists(string reg_ip, int regctrl)
        {
            return dal.Exists(reg_ip, regctrl);
        }

        /// <summary>
        /// 根据用户名密码返回一个实体
        /// </summary>
        /// <param name="user_name">用户名(明文)</param>
        /// <param name="password">密码</param>
        /// <param name="emaillogin">是否允许邮箱做为登录</param>
        /// <param name="mobilelogin">是否允许手机做为登录</param>
        /// <param name="is_encrypt">是否需要加密密码</param>
        /// <returns></returns>
        //public Model.users GetModel(string user_name, string password, int emaillogin, int mobilelogin, bool is_encrypt)
        //{
        //    //检查一下是否需要加密
        //    if (is_encrypt)
        //    {
        //        //先取得该用户的随机密钥
        //        string salt = dal.GetSalt(user_name);
        //        if (string.IsNullOrEmpty(salt))
        //        {
        //            return null;
        //        }
        //        //把明文进行加密重新赋值
        //        password = DESEncrypt.Encrypt(password, salt);
        //    }
        //    return dal.GetModel(user_name, password, emaillogin, mobilelogin);
        //}

        public Model.users GetModel(string user_name, string password, int emaillogin, int mobilelogin, bool is_encrypt)
        {
            return dal.GetModel(user_name, password, emaillogin, mobilelogin);
        }

        /// <summary>
        /// 根据用户名返回一个实体
        /// </summary>
        public Model.users GetModel(string user_name)
        {
            return dal.GetModel(user_name);
        }

        /// <summary>
        /// 根据用户名返回一个实体
        /// </summary>
        public Model.users GetMobileModel(string mobile)
        {
            return dal.GetMobileModel(mobile);
        }

        /// <summary>
        /// 检查Email是否存在
        /// </summary>
        public bool ExistsEmail(string email)
        {
            return dal.ExistsEmail(email);
        }

        /// <summary>
        /// 检查手机号码是否存在
        /// </summary>
        public bool ExistsMobile(string mobile)
        {
            return dal.ExistsMobile(mobile);
        }

        /// <summary>
        /// 检查手机号码是否存在
        /// </summary>
        public int CheckUserState(string mobile)
        {
            return dal.CheckUserState(mobile);
        }
        /// <summary>
        /// 根据用户ID获取用户是否管理员
        /// </summary>
        public int CheckUserAdminState(int user_id)
        {
            return dal.CheckUserAdminState(user_id);
        }

        /// <summary>
        /// 返回一个随机用户名
        /// </summary>
        public string GetRandomName(int length)
        {
            string temp = Utils.Number(length, true);
            if (Exists(temp))
            {
                return GetRandomName(length);
            }
            return temp;
        }

        /// <summary>
        /// 根据用户名取得Salt
        /// </summary>
        public string GetSalt(string user_name)
        {
            return dal.GetSalt(user_name);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public int UpdateField(int id, string strValue)
        {
            return dal.UpdateField(id, strValue);
        }
        /// <summary>
        /// 获取随机userid
        /// </summary>
        public int GetRandomUserID(string strWhere)
        {
            return dal.GetRandomUserID(strWhere);
        }

       
        #endregion
    }
}